package com.woniuxy.microsportsserver.walkmag.controller;

import com.woniuxy.common.bean.PageDto;
import com.woniuxy.common.bean.ResultMsg;
import com.woniuxy.microsportsserver.bean.WalkAnalysisInfo;
import com.woniuxy.microsportsserver.bean.WalkInfo;
import com.woniuxy.microsportsserver.walkmag.service.IWalkService;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RequestMapping("/walk")
@RestController
public class WalkController {
    @Resource
    private IWalkService walkService;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private ListOperations listOperations;

    //查询页面一数据步数的总和
    @GetMapping(value="/all",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg findAll(WalkInfo walkInfo , Integer pageNum, Integer pageSize){
        ResultMsg resultMsg = new ResultMsg();
        PageDto pageDto = walkService.findAll(walkInfo,pageNum, pageSize);
        if (pageDto == null) {
            return resultMsg.setCode(10000).setMessage("系统繁忙，请稍后再试");
        }
        return resultMsg.setCode(200).setMessage("查询成功").setData(pageDto);
    }



    //查询一个人具体某一天或所有的步数记录
    @GetMapping(value = "/allRecords",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg allRecords(WalkInfo walkInfo){
        List<WalkInfo> all = walkService.findRecordsByUserIdOrTime(walkInfo);
        return new ResultMsg().setCode(200).setMessage("操作成功").setData(all);
    }


    //查询近一周的平均数
    @GetMapping(value = "/avgWalk",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg avgWalk(){
        List<WalkAnalysisInfo> datas = walkService.findNumAvgBytimeType();
//      /*  listOperations.leftPushAll("walkavg",datas);
        //将上述的stus中数据，让它在Redis服务中保存 6天
  //      redisTemplate.expire("stus",6, TimeUnit.DAYS);
        //删除user这么一个key
   //     redisTemplate.delete("user");
  //      System.out.println(redisTemplate.type("stus"));*/
        return new ResultMsg().setCode(200).setMessage("操作成功").setData(datas);
    }


    //查询近一周的平均数
    @GetMapping(value = "/WalkPercentage",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg WalkPercentage(){
        List<WalkAnalysisInfo> datas = walkService.findWalkPercentageBYUser();
        return new ResultMsg().setCode(200).setMessage("操作成功").setData(datas);
    }

    //top前是的数据
    @GetMapping(value = "/walkTop",produces = MediaType.APPLICATION_JSON_VALUE)
    public ResultMsg walkTop(){
        List<WalkAnalysisInfo> datas = walkService.findTop10ByTimeType(new WalkAnalysisInfo());
        return new ResultMsg().setCode(200).setMessage("操作成功").setData(datas);
    }


}
